package com.bd.scm.module.log.contorller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.core.toolkit.SwaggerConstants;
import com.bd.scm.domain.dto.login.SysLog;
import com.bd.scm.domain.dto.login.SysLogDto;
import com.bd.scm.domain.dto.login.SysLogExport;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.log.service.BdSysLogService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.StringUtils;
import com.bd.utils.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.UUID;

/** 系统日志 Controller
 * @author YH
 * @version 1.0
 * @date 2020/12/17 16:05
 */
@RestController
@RequestMapping("/api/sys/log")
@Api(tags = "系统日志接口")
@ApiSort(value = SwaggerConstants.ORDER_MODULE_VACATION)
public class BdSysLogController extends BaseController {

    @Autowired
    private BdSysLogService bdSysLogService;

    /**
     * 查询日志
     *
     * @return
     */
    @ApiOperation(value = "查询日志", httpMethod = "POST", protocols = "HTTP", notes = "查询日志", produces = "application/json")
    @ApiOperationSupport(order = 1)
    @PostMapping("getBdSysLogList")
    public Result getBdSysLogList(@RequestBody SysLog sysLog, HttpServletRequest request) {
        Result result = null;
        Page<SysLogDto> page = new Page<>(sysLog.getCurrent(),
                sysLog.getPageSize());
        IPage<SysLogDto> pageList = this.bdSysLogService.getBdSysLogList(page, sysLog);
        result = ResultUtil.ok(pageList);
        return result;
    }

    /**
     * 导出日志
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping("exportBdSysLogList")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "导出日志", httpMethod = "GET", protocols = "HTTP", notes = "导出日志", produces =
            "application/text")
    public Result exportBdSysLogList(HttpServletRequest request, HttpServletResponse response, String jsonIn) throws Exception {
        SysLog sysLog = new SysLog();
        if(!StringUtils.isEmpty(jsonIn)){
            jsonIn = URLDecoder.decode(jsonIn, "UTF-8");
            sysLog =
                    (SysLog) JSONObject.toJavaObject(JSON.parseObject(jsonIn), SysLog.class);
        }
        sysLog.setCurrent(1);
        sysLog.setPageSize(Integer.MAX_VALUE);
        List<SysLogExport> list = this.bdSysLogService.exportBdSysLogList(sysLog);
        ExcelUtil.ExportExcel("系统日志" + UUID.randomUUID().toString(), response, list, SysLogExport.class);
        return ResultUtil.ok();
    }

    /**
     * 获取操作类型
     * @return
     */
    @GetMapping("getOperationList")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "获取操作类型", httpMethod = "GET", protocols = "HTTP", notes = "获取操作类型", produces =
            "application/text")
    public Result getOperationList() {
        return ResultUtil.ok(this.bdSysLogService.getOperationList());
    }
}
